package de.baumann.browser.adapter

import android.annotation.SuppressLint
import android.content.Context
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.RelativeLayout
import android.widget.TextView
import de.baumann.browser.R
import de.baumann.browser.api.net.vo.Card

class CardListAdapter(var context: Context, var onItemClickListener: RvItemClickListener) : RecyclerView.Adapter<CardListAdapter.CardHolder>() {

    private var cards: List<Card>? = null

    fun setCards(cards: List<Card>) {
        this.cards = cards
        notifyDataSetChanged()
    }

    fun getCard(position: Int): Card {
        return cards!![position]
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardHolder {
        val view = LayoutInflater.from(context).inflate(R.layout.item_card, parent, false)
        return CardHolder(view)
    }

    override fun getItemCount(): Int {
        return if (cards == null) {
            0
        } else {
            cards!!.size
        }
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: CardHolder, position: Int) {
        val card = cards!![position]
        holder.cardName.text = card.cardName
        holder.cardPrice.text = card.cardPrice + "ODIN"

        var bg = R.drawable.blackcard_bg_black
        var cardTextColor = R.color.windowLight
        var powerTextColor = R.color.color_999999
//        var drawableRight = R.drawable.blackcard_icon_more
        when (card.cardType) {
            0 -> {
                bg = R.drawable.blackcard_bg_black
                cardTextColor = R.color.windowLight
                powerTextColor = R.color.color_999999
//                drawableRight = R.drawable.blackcard_icon_more
                val surplusNum = cards!![position].surplusNum

                if (surplusNum<=100){
                    holder.surplusNum.text = "剩余${surplusNum}份"
                }

                if(surplusNum<=0){
                    holder.surplusNum.visibility = View.INVISIBLE
                }else{
                    holder.surplusNum.visibility = View.VISIBLE
                }
            }
            1 -> {
                bg = R.drawable.blackcard_bg_gold
                cardTextColor = R.color.color_black_121212
                powerTextColor = R.color.color_666666
//                drawableRight = R.drawable.blackcard_icon_black
            }
            2 -> {
                bg = R.drawable.blackcard_bg_white
                cardTextColor = R.color.color_black_121212
                powerTextColor = R.color.color_666666
//                drawableRight = R.drawable.blackcard_icon_black
            }
        }

        holder.rlRoot.setBackgroundResource(bg)
        holder.cardName.setTextColor(context.resources.getColor(cardTextColor))
        holder.cardEndTime.setTextColor(context.resources.getColor(cardTextColor))
        holder.cardPrice.setTextColor(context.resources.getColor(cardTextColor))
        holder.cardPower.setTextColor(context.resources.getColor(cardTextColor))
        /* val drawable = context.resources.getDrawable(drawableRight)
         drawable.setBounds(0, 0, drawable.minimumWidth, drawable.minimumHeight)
         holder.cardName.setCompoundDrawables(null, null, drawable, null)*/
        holder.rvCardPower.adapter = CardPowerAdapter(context, card.cardPower!!, powerTextColor)

    }

    inner class CardHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var rlRoot: RelativeLayout = itemView.findViewById(R.id.itemRoot)
        var cardPower: TextView = itemView.findViewById(R.id.cardPower)
        var rvCardPower: RecyclerView = itemView.findViewById(R.id.rvCardPower)
        var cardEndTime: TextView = itemView.findViewById(R.id.cardEndTime)
        var cardPrice: TextView = itemView.findViewById(R.id.cardPrice)
        var cardName: TextView = itemView.findViewById(R.id.cardName)
        private var btnBuyCard: Button = itemView.findViewById(R.id.btnListBuyCard)
        var surplusNum: TextView = itemView.findViewById(R.id.surplusNum)

        init {
            rvCardPower.layoutManager = LinearLayoutManager(context)

            itemView.setOnClickListener {
                onItemClickListener.onClick(layoutPosition)
            }

            btnBuyCard.setOnClickListener {
                onItemClickListener.onClick(layoutPosition)
            }
        }
    }
}